package service;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Streamutil {

    /**
     * 字节接收(读取)流转换为字符串工具方法
     * 
     * @param in
     * @return
     */
    public static String StreamToString(InputStream in) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        StringBuilder date = new StringBuilder();
        String line ;

        while ((line = br.readLine()) != null) {
            date.append(line);
        }

        return date.toString();
    }

    /**
     * 读取字节流信息返回字节数组
     * 
     * @param in
     * @return
     * @throws IOException
     */
    public static byte[] streamtoArray(InputStream in) throws IOException {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        byte temp[] = new byte[1024];
        int readLen = 0;

        while ((readLen = in.read(temp)) != -1) {// 读取传入信息,写到bytearr对象
            bo.write(temp, 0, readLen);
        }
        byte data[] = bo.toByteArray();

        return data;
    }

}
